import collection from "../../../api/collection";

// pages/Exhi/exhibitionDetail/exhibitionDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFavorite: 0,
    scale: 1,
    collection: {},
    relatedItems: []
  },


  // 收藏/取消收藏
  toggleFavorite() {
    console.log(this.data.isFavorite)
    if (this.data.isFavorite == 0) {
      collection.likeCollection(this.data.collection.id).then(res => {
        if (res.code == 200) {
          this.setData({
            isFavorite: 1
          });
        }
      }).catch(err => {
        console.log(err)
      })
    } else {
      collection.cancelLikeCollection(this.data.collection.id).then(res => {
        if (res.code == 200) {
          this.setData({
            isFavorite: 0
          });
        }
      }).catch(err => {
        console.log(err)
      })
    }
  },

  // 预约参观
  bookVisit() {
    wx.navigateTo({
      url: '/pages/booking/index'
    });
  },

  // 查看相关藏品
  viewRelated(e) {
    const name = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/Exhi/exhibitionDetail/exhibitionDetail?name=${name}`
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const name = options.name;
    console.log("页面加载")
    console.log(name)
    await collection.getCollectionDetail(name).then(res => {
      this.setData({
        collection: res.collection,
        relatedItems: res.randomCollection
      })
    }).catch(err => {
      console.log(err)
    })
    console.log(this.data.collection.id)
    await collection.isLikeCollection(this.data.collection.id).then(res => {
      if (res.code == 500) {
        this.setData({
          isFavorite: 1
        })
        console.log("点赞情况")
        console.log(this.data.isFavorite)
      }
    }).catch(err=>{
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})